use tokio_tungstenite::{connect_async, tungstenite::Message};
use serde_json::json;
use futures_util::{SinkExt, StreamExt};


#[tokio::main]
pub async fn send_message_test(target: i32, message: &str, mode: &str) -> Result<(), Box<dyn std::error::Error>> {
    // 1. 连接 NapCat 的 WebSocket 服务
    let ws_url = "ws://127.0.0.1:8080/ws";  // 根据实际配置调整
    let (ws_stream, _) = connect_async(ws_url).await?;
    let (mut write, mut read) = ws_stream.split();

    // 2. 构造 OneBot 协议消息
    let msg = match mode {
        "1" => json!({
            "action": "send_group_msg",
            "params": {
                "group_id": target,  // 替换为群号
                "message": message
            }
        }),
        _ => json!({
            "action": "send_private_msg",
            "params": {
                "user_id": target,  // 替换为好友QQ号
                "message": message
            }
        })
    };

    // 3. 发送消息
    write.send(Message::Text(msg.to_string())).await?;
    println!("[Sent] Message \"{}\" to QQ", message);

    // 4. 接收响应（可选）
    if let Some(Ok(Message::Text(response))) = read.next().await {
        println!("[Received] Response: {}", response);
    }

    Ok(())
}